package com.xx.pmis.web.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xx.pmis.model.Subject;
import com.xx.pmis.model.util.SubjectTree;
import com.xx.pmis.services.AccountService;
import com.xx.pmis.services.SubjectService;


/**
 * @class 构建科目树，用于记账记录展示 <br>
 * 
 * @editor 石星星 @Date 2011-9-23 上午10:47:29 <br>
 * @version jdk_1.5.0
 */
public class DynamicAccountTree extends PMISBaseUtil {
	private static final Log log = LogFactory.getLog(DynamicAccountTree.class);
	
	AccountService accountService = getPMISServices().getAccountService();
	SubjectService subjectService = getPMISServices().getSubjectService();

	/**
	 * 记账本记账记录树
	 * 
	 * @return
	 */
	public List<SubjectTree> MadeAccountTree() {
		int id = 2;
		List<SubjectTree> subjectTreeList = new ArrayList<SubjectTree>();
		Subject subject = new Subject();
		Subject subject2 = new Subject();

		List<Subject> subjectList = null;
		try {
			subjectList = subjectService.getAllSubjectByCondition(" where subParentID = 0 order by subCode asc");
		} catch (Exception e) {
			log.info("【财务管理/记账本/科目】查询科目异常！" + e.toString());
		}

		// 科目
		for (Iterator<?> it = subjectList.iterator(); it.hasNext();) {
			SubjectTree subTree1 = new SubjectTree();
			subject = (Subject) it.next();
			subTree1.setId(id++);
			subTree1.setParid(1);
			subTree1.setName(subject.getSubName());
			subTree1.setUrl("");
			subTree1.setTitle(subject.getSubName());
			subjectTreeList.add(subTree1);
			
			// 二级科目
			if (!subject.getSubID().equals("") && subject.getSubID() != "") {

				List<Subject> subjectList2 = null;
				try {
					subjectList2 = subjectService.getAllSubjectByCondition(" where subParentID = '" + subject.getSubID()  + "'");
				} catch (Exception e) {
					log.info("查询二级科目异常！" + e.toString());
				}
				int parid = id - 1;
				for (Iterator<?> it2 = subjectList2.iterator(); it2.hasNext();) {
					SubjectTree subTree2 = new SubjectTree();
					subject2 = (Subject) it2.next();
					subTree2.setId(id++);
					subTree2.setParid(parid);
					subTree2.setName(subject2.getSubName());
					subTree2.setUrl("account.do?method=listAccountBySubjectID&subjectID=" + subject2.getSubID());
					subTree2.setTitle(subject2.getSubName());
					subTree2.setTarget("");
					subjectTreeList.add(subTree2);

				}
			}

		}
		return subjectTreeList;
	}
}
